home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / nild.exe / INPTEST.CPP < prev    next >
C/C++ Source or Header  |  1992-08-29  |  4KB  |  180 lines

  1. //
  2. //  INPTEST.CPP
  3. //
  4. //    Test program for NumInputLine
  5. //
  6.  
  7. #define Uses_MsgBox
  8. #define Uses_TEvent
  9. #define Uses_TApplication
  10. #define Uses_TKeys
  11. #define Uses_TRect
  12. #define Uses_TMenu
  13. #define Uses_TMenuBar
  14. #define Uses_TMenuItem
  15. #define Uses_TStatusLine
  16. #define Uses_TStatusItem
  17. #define Uses_TStatusDef
  18. #define Uses_TDeskTop
  19. #define Uses_TDialog
  20. #define Uses_TLabel
  21. #define Uses_TButton
  22. #include <tv.h>
  23.  
  24. #include <strstream.h>
  25. #include "numinp.h"
  26.  
  27. const cmAbout = 100;
  28. const cmTest = 101;
  29.  
  30. class Shell : public TApplication {
  31.  
  32. public:
  33.  
  34.   Shell();
  35.   static TMenuBar *initMenuBar( TRect r );
  36.   static TStatusLine *initStatusLine( TRect r );
  37.   void handleEvent( TEvent& event );
  38.   void idle();
  39.  
  40. private:
  41.  
  42.   void About();
  43.   void Test();
  44. };
  45.  
  46. Shell::Shell() : TProgInit (
  47.       &Shell::initStatusLine,
  48.       &Shell::initMenuBar,
  49.       &Shell::initDeskTop
  50.       )
  51. {
  52. }
  53.  
  54. TMenuBar *Shell::initMenuBar( TRect r )
  55. {
  56.   r.b.y =  r.a.y + 1;
  57.  
  58.   TMenuItem& mI =
  59.     *new TMenuItem( "~T~est", cmTest, kbNoKey, hcNoContext );
  60.  
  61.   return new TMenuBar( r, new TMenu( mI ) );
  62. }
  63.  
  64.  
  65. TStatusLine *Shell::initStatusLine( TRect r )
  66. {
  67.   r.a.y = r.b.y - 1;
  68.  
  69.   TStatusLine *sl = new TStatusLine( r,
  70.     *new TStatusDef(0, 0xFFFF) +
  71.       *new TStatusItem( 0, kbF10, cmMenu ) +
  72.       *new TStatusItem( "~Alt-X~ Quit", kbAltX, cmQuit ) );
  73.  
  74.   return sl;
  75. }
  76.  
  77. void Shell::handleEvent( TEvent &event )
  78. {
  79.   TApplication::handleEvent(event);
  80.  
  81.   if (event.what == evCommand)
  82.   {
  83.     switch (event.message.command)
  84.     {
  85.       case cmAbout:
  86.         About();
  87.         break;
  88.       case cmTest:
  89.         Test();
  90.         break;
  91.       default:
  92.         break;
  93.     }
  94.   }
  95. }
  96.  
  97. void Shell::idle()
  98. {
  99.   // select menu bar if deskTop empty
  100.  
  101.   if( deskTop->current == 0
  102.   && !menuBar->getState( sfSelected ) )
  103.   {
  104.     TEvent event;
  105.     event.what = evCommand;
  106.     event.message.command = cmMenu;   // put a cmMenu event in queue
  107.     putEvent( event );
  108.   }
  109. }
  110.  
  111. void Shell::About()
  112. {
  113.   messageBox( "\003Numeric Input Line Demo",
  114.     mfInformation | mfOKButton );
  115. }
  116.  
  117.  
  118.  
  119.  
  120. // structure to get data into and out of the dialog box.
  121. // Note that the structure contains the types we're interested
  122. // in, not strings, as would be need for a normal TInputLine.
  123.  
  124. struct IntDbl {
  125.   int ival;
  126.   double dval;
  127. };
  128.  
  129.  
  130. // Build a dialog box demonstrating IntInputLine and DoubleInputLine
  131.  
  132. void Shell::Test()
  133. {
  134.   TDialog *d = new TDialog( TRect(0,0,30,10), "Test" );
  135.   d->options |= ofCentered;
  136.  
  137.   IntInputLine *il = new IntInputLine( TRect(11,2,21,3), 6 );
  138.   d->insert( new TLabel( TRect(2,2,10,3), "Integer", il ) );
  139.   d->insert( il );
  140.  
  141.   DoubleInputLine *dl = new DoubleInputLine( TRect(11,4,21,5), 8 );
  142.   d->insert( new TLabel( TRect(2,4,10,5), "Double", dl ) );
  143.   d->insert( dl );
  144.  
  145.   d->insert( new TButton( TRect(10,7,20,9), "O~K~", cmOK, bfDefault ) );
  146.   il->select();
  147.  
  148.   IntDbl buf = { 123, 1.23 };           // stuff some initial values
  149.   d->setData( &buf );
  150.  
  151.   ushort cmd = deskTop->execView( d );  // execute the dialog
  152.  
  153.   if( cmd != cmCancel )
  154.   {
  155.     d->getData( &buf );                 // read the input values
  156.  
  157.     char msg[40];                       // display them
  158.     ostrstream os( msg, 40 );
  159.     os << "\003You entered: " << buf.ival
  160.        << " and " << buf.dval << ends;
  161.  
  162.     messageBox( os.str(), mfInformation | mfOKButton );
  163.   }
  164.  
  165.   destroy( d );                          // clean up
  166. }
  167.  
  168. int main()
  169. {
  170.   TEvent init;
  171.   init.what = evCommand;
  172.   init.message.command = cmAbout;     // make a cmAbout command event
  173.  
  174.   Shell shell;
  175.   shell.putEvent(init);               // put it in the queue to pop up
  176.   shell.run();                        // About box when program starts
  177.   return 0;
  178. }
  179.  
  180.